package nl.unimaas.micc.umsnooker.gameflow.phases;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;

import nl.unimaas.micc.umsnooker.math.BairstowSolver;
import nl.unimaas.micc.umsnooker.presentation.SnookerTable3D;
import nl.unimaas.micc.umsnooker.presentation.TablePresentation;
import nl.unimaas.micc.umsnooker.simulation.Simulation;
import nl.unimaas.micc.umsnooker.simulation.event.BallCollision;
import nl.unimaas.micc.umsnooker.simulation.event.NoisyCueInteraction;
import nl.unimaas.micc.umsnooker.simulation.physics.Physics;

import com.jme.app.AbstractGame.ConfigShowMode;
import com.jme.math.Vector2f;
import com.jme.math.Vector3f;

import nl.unimaas.micc.umsnooker.control.ControllingUnit;
import nl.unimaas.micc.umsnooker.control.ai.RandomAIControl;
import nl.unimaas.micc.umsnooker.control.ui.ManualGameControl;
import nl.unimaas.micc.umsnooker.domain.BilliardBall;
import nl.unimaas.micc.umsnooker.domain.Player;
import nl.unimaas.micc.umsnooker.domain.Table;
import nl.unimaas.micc.umsnooker.exec.UMSnooker;
import nl.unimaas.micc.umsnooker.gameflow.GameFlow;
import nl.unimaas.micc.umsnooker.gameflow.GamePhase;
import nl.unimaas.micc.umsnooker.gameflow.GameState;

public class GameInitPhase extends GamePhase
{
	public GameInitPhase(GameState state)
	{
		super(state);
	}

	@Override
	public void start()
	{
		super.start();
		
		setDefaults();
		initNoise();
		initThresholds();
		finish();
	}

	private void initThresholds()
	{
		BairstowSolver.THRESHOLD = 5E-6;
		Simulation.TIME_THRESHOLD = 1E-7;
		Simulation.SPACE_THRESHOLD = 1E-6;
		BallCollision.VELOCITY_THRESHOLD = 1E-8f;
		Physics.THRESHOLD = 5E-6;
		
		
	}

	private void initNoise()
	{
		NoisyCueInteraction.destDev = .001f;
		NoisyCueInteraction.elevDev = .001f;
		NoisyCueInteraction.transXDev = .0001f;
		NoisyCueInteraction.transYDev = .0001f;
		NoisyCueInteraction.velDev = .01f;
	}

	private void setDefaults()
	{
		// --> Initialize the table
		Table table = new Table(1.778f, 3.569f, .737f, .292f, .324f, .0525f/2f, .045f, .045f);
		setTable(table);
				
		// --> Initialize presentation
		TablePresentation presentation = initPresentation();
		
		// --> Initialize players
		Player p1 = new Player();
		p1.setName("Hendry");
		p1.setControllingUnit(new ManualGameControl(presentation,state));
		p1.setScore(0);
		
		Player p2 = new Player();
		p2.setName("O'Sullivan");
		p2.setControllingUnit(new ManualGameControl(presentation,state));
		p2.setScore(0);

		
		List<Player> players = new ArrayList<Player>();
		players.add(p1);
		players.add(p2);
		setPlayers(players);
		setCurrentPlayer(state.getPlayers().get(0));
	}
	
	private TablePresentation initPresentation()
	{
		TablePresentation presentation = new SnookerTable3D(state);
		presentation.setConfigShowMode(ConfigShowMode.AlwaysShow, UMSnooker.class
				.getClassLoader().getResource("nl/unimaas/micc/umsnooker/image/snooker.png"));
		
		
		(new Thread(presentation)).start();
		
		while (!presentation.isReady())
		{
			try
			{
				Thread.sleep(1000);
			} catch (InterruptedException e)
			{
				e.printStackTrace();
			}			
		}
		try
		{
			Thread.sleep(1000);
		} catch (InterruptedException e)
		{
			e.printStackTrace();
		}
		
		return presentation;
	}

	
	public void setCurrentPlayer(Player current)
	{
		state.setCurrentPlayer(current);
	}
	
	public void setTable(Table table)
	{
		state.setTable(table);
	}
		
	public void setPlayers(List<Player> players)
	{
		state.setPlayers(players);
	}
	
	@Override
	public void finish()
	{
		if(finished)
		{
			return;
		}
		setNext(new FrameInitPhase(state));
		super.finish();
	}
}
